using System;
using LitJson;
using UnityEngine;
#if UNITY_WEBGL
using System.Runtime.InteropServices;
#endif

namespace CGF
{
    public class JSHelper : MonoBehaviour
    {
#if UNITY_WEBGL
        [DllImport("__Internal")]
        private static extern IntPtr Web_GetCurrentDomain();
        [DllImport("__Internal")]
        private static extern IntPtr Web_GetCurrentFullURL();
        [DllImport("__Internal")]
        private static extern IntPtr Web_GetAllURLParameters();
        [DllImport("__Internal")]
        private static extern int Web_GetWindowInnerWidth();
        [DllImport("__Internal")]
        private static extern int Web_GetWindowInnerHeight();
#endif

        /// <summary>
        /// 获取包括http的域名
        /// </summary>
        public static string GetCurrentDomain()
        {
#if UNITY_WEBGL
            IntPtr buffer = IntPtr.Zero;
            try
            {
                buffer = Web_GetCurrentDomain();
                if (buffer == IntPtr.Zero)
                {
                    return string.Empty;
                }
                string result = Marshal.PtrToStringUTF8(buffer);
                return result ?? string.Empty;
            }
            catch (Exception e)
            {
                Debug.LogError($"GetCurrentDomain()失败: {e.Message}");
                return string.Empty;
            }
            finally
            {
                if (buffer != IntPtr.Zero) Marshal.FreeHGlobal(buffer);
            }
#else
            return string.Empty;
#endif
        }

        public static string GetCurrentFullURL()
        {
#if UNITY_WEBGL
            IntPtr buffer = IntPtr.Zero;
            try
            {
                buffer = Web_GetCurrentFullURL();
                if (buffer == IntPtr.Zero)
                {
                    return string.Empty;
                }
                string result = Marshal.PtrToStringUTF8(buffer);
                return result ?? string.Empty;
            }
            catch (Exception e)
            {
                Debug.LogError($"GetCurrentFullURL()失败: {e.Message}");
                return string.Empty;
            }
            finally
            {
                if (buffer != IntPtr.Zero) Marshal.FreeHGlobal(buffer);
            }
#else
            return string.Empty;
#endif
        }

        public static JsonData GetAllURLParameters()
        {
#if UNITY_WEBGL
            IntPtr buffer = IntPtr.Zero;
            try
            {
                buffer = Web_GetAllURLParameters();
                if (buffer == IntPtr.Zero) return string.Empty;
                string result = Marshal.PtrToStringUTF8(buffer);
                if (string.IsNullOrEmpty(result)) return null;
                return JsonMapper.ToObject(result);
            }
            catch (Exception e)
            {
                Debug.LogError($"GetAllURLParameters()失败: {e.Message}");
                return null;
            }
            finally
            {
                if (buffer != IntPtr.Zero) Marshal.FreeHGlobal(buffer);
            }
#else
            return null;
#endif
        }
        
        //获取浏览器视口宽度
        public static int GetWindowInnerWidth()
        {
#if UNITY_WEBGL
            try
            {
                return Web_GetWindowInnerWidth();
            }
            catch (Exception e)
            {
                Debug.LogError($"GetWindowInnerWidth()失败: {e.Message}");
                return 0;
            }
#else
            return 0;
#endif
        }
        
        //获取浏览器视口高度
        public static int GetWindowInnerHeight()
        {
#if UNITY_WEBGL
            try
            {
                return Web_GetWindowInnerHeight();
            }
            catch (Exception e)
            {
                Debug.LogError($"GetWindowInnerHeight()失败: {e.Message}");
                return 0;
            }
#else
            return 0;
#endif
        }
        
    }
}
